package com.ruoyi.project.yuwen.topman.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.yuwen.kkuserInfo.domain.YuwenKkuserInfo;
import com.ruoyi.project.yuwen.kkuserInfo.service.IYuwenKkuserInfoService;
import com.ruoyi.project.yuwen.topman.domain.AudioTopman;
import com.ruoyi.project.yuwen.topman.mapper.AudioTopmanMapper;
import com.ruoyi.project.yuwen.topman.service.IAudioTopmanService;
import com.ruoyi.project.yuwen.topmanlog.domain.AudioTopmanLog;
import com.ruoyi.project.yuwen.topmanlog.mapper.AudioTopmanLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 朗讼达人Service业务层处理
 *
 * @author shixiangwen
 * @date 2021-03-09
 */
@Service
@DataSource(DataSourceType.KKCUSER)
public class AudioTopmanServiceImpl implements IAudioTopmanService {

    @Autowired
    private AudioTopmanMapper audioTopmanMapper;

    @Autowired
    private AudioTopmanLogMapper audioTopmanLogMapper;


    @Autowired
    IYuwenKkuserInfoService kkuserInfoService;


    /**
     * 查询朗讼达人
     *
     * @param id 朗讼达人ID
     * @return 朗讼达人
     */
    @Override
    public AudioTopman selectAudioTopmanById(Long id) {
        AudioTopman audioTopman = audioTopmanMapper.selectAudioTopmanById(id);
        if (audioTopman != null) {
            YuwenKkuserInfo kkuserInfo = kkuserInfoService.selectKkuserInfoById(audioTopman.getKkuid());
            if (kkuserInfo != null) {
                audioTopman.setNickname(kkuserInfo.getUname());
            }
        }
        return audioTopman;
    }

    /**
     * 查询朗讼达人列表
     *
     * @param audioTopman 朗讼达人
     * @return 朗讼达人
     */
    @Override
    public List<AudioTopman> selectAudioTopmanList(AudioTopman audioTopman) {
        List<AudioTopman> audioTopmenList = audioTopmanMapper.selectAudioTopmanList(audioTopman);
        audioTopmenList.forEach(item -> {
            YuwenKkuserInfo kkuserInfo = kkuserInfoService.selectKkuserInfoById(item.getKkuid());
            if (kkuserInfo != null) {
                item.setNickname(kkuserInfo.getUname());
            }
        });
        return audioTopmenList;
    }

    /**
     * 新增朗讼达人
     *
     * @param audioTopman 朗讼达人
     * @return 结果
     */
    @Override
    public int insertAudioTopman(AudioTopman audioTopman) {
        return audioTopmanMapper.insertAudioTopman(audioTopman);
    }

    /**
     * 修改朗讼达人
     *
     * @param audioTopman 朗讼达人
     * @return 结果
     */
    @Override
    @Transactional
    public int updateAudioTopman(AudioTopman audioTopman, int upType, String remark) {
        int effect = audioTopmanMapper.updateAudioTopman(audioTopman);

        AudioTopman old = audioTopmanMapper.selectAudioTopmanById(audioTopman.getId());

        if (upType > 0) {
            AudioTopmanLog audioTopmanLog = new AudioTopmanLog();
            audioTopmanLog.setActivePraise((long) old.getActivePraise());
            audioTopmanLog.setCreatedTime(DateUtils.getNowDate());
            audioTopmanLog.setKkuid(old.getKkuid());
            audioTopmanLog.setLevel(audioTopman.getLevel());
            audioTopmanLog.setType((long) upType);
            audioTopmanLog.setUpdatedTime(DateUtils.getNowDate());
            audioTopmanLog.setRemark(remark);
            audioTopmanLogMapper.insertAudioTopmanLog(audioTopmanLog);
        }

        YuwenKkuserInfo kkuserInfo = kkuserInfoService.selectKkuserInfoById(audioTopman.getKkuid());
        if (kkuserInfo != null) {
            kkuserInfo.setTopmanLevel(audioTopman.getLevel().intValue());
            kkuserInfoService.updateKkuserInfo(kkuserInfo);
        }

        return effect;
    }

    /**
     * 删除朗讼达人对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAudioTopmanByIds(String ids) {
        return audioTopmanMapper.deleteAudioTopmanByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除朗讼达人信息
     *
     * @param id 朗讼达人ID
     * @return 结果
     */
    @Override
    public int deleteAudioTopmanById(Long id) {
        return audioTopmanMapper.deleteAudioTopmanById(id);
    }
}
